<?php
require_once(dirname(__FILE__) . '/../../inc/config.inc.php');
require_once(dirname(__FILE__) . '/../../function.php');
require_once('PHPExcel.php');


$dtime = isset($_GET['dtime']) ? htmlspecialchars($_GET['dtime']) : "";   //用车人
$dtime = trim($dtime);
$school = isset($_GET['school']) ? htmlspecialchars($_GET['school']) : '';   //商家id
$mobile = isset($_GET['mobile']) ? htmlspecialchars($_GET['mobile']) : '';   //商家id
$manageid = isset($_GET['manageid']) ? intval($_GET['manageid']) : 0;   //用户的身份

$data = [];

$sql="SELECT *  FROM `#@__visit` where 1   ";


if ($manageid>0) {
    $sql .= "  and uid='$manageid'  ";
}
if ($dtime != "") {
    $time_arr = explode("~", $dtime);
    if (is_array($time_arr) && count($time_arr) == 2) {
        $start_time = strtotime(trim($time_arr[0]));
        $end_time = strtotime(trim($time_arr[1]))+ 3600 * 24 - 1;
        $sql .= " AND post_time>'$start_time' AND post_time<'$end_time' ";

    }
}

$new_item = [];
$top_title = "";
$sql.=" order by id desc";
$dosql->Execute($sql);
while ($row = $dosql->GetArray()) {
    $orderid=$row['id'];
    $typename="拜访";



    $row['addtime'] = GetDateTime($row['post_time']);
    $row['mtruename'] = getManage($row['uid']);
    $row['ordertype'] = $typename;

    $row_arr['data'][] = $row;
    $new_item[] = $row;

}

//echo $sql;
//var_dump($new_item);
//exit;

$cellKey = array(
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
    'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'
);
//序号	省	市	区	报修单位	联系人	联系方式	设备类型	序列号	故障描述	维修进度	维修费用	报修时间	客服联系备注	完修时间	维修概要	完修工程师	备注 报修地址 报修地址说明  完修地址 完修地址说明  报修到完修距离 报修到服务站距离
$objPHPExcel = new PHPExcel();// 操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);// 设置sheet名
$objPHPExcel->getActiveSheet()->setTitle("中奖列表");// 设置表格宽度

$cellName = array(
    'id' => array('id', 'id', 8), //列名称，索引值，宽度
    'mtruename' => array('工程师', 'mtruename', 10), //列名称，索引值，宽度
    'ordertype' => array('大项', 'ordertype', 12), //列名称，索引值，宽度
    'school' => array('单位', 'school', 25), //列名称，索引值，宽度
    'truename' => array('联系人', 'truename', 16), //列名称，索引值，宽度
    'mobile' => array('手机', 'mobile', 16), //列名称，索引值，宽度
    'address' => array('地址', 'address', 40), //列名称，索引值，宽度
    'content' => array('任务摘要', 'content', 40), //列名称，索引值，宽度
    'addtime' => array('操作时间', 'addtime', 20),
);


$topNumber = 2;//表头有几行占用

//创建一个excel对象
$objPHPExcel = new PHPExcel();

$styleThinBlackBorderOutline = array(
    'borders' => array(
        'allborders' => array( //设置全部边框
            'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
        ),

    ),
);


// 设置第一行
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
$objPHPExcel->getActiveSheet()->mergeCells('A1:' . $cellKey[count($cellName) - 1] . '1');
//合并单元格（如果要拆分单元格是需要先合并再拆分的，否则程序会报错）
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $top_title . "拜访列表\n" . $dtime);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15);
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(50);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);


//$objPHPExcel->getActiveSheet()->getStyle('T')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
//处理表头

$cellName_num = count($cellName);
$lastCell = $cellKey[$cellName_num - 1]; //获取最后一行的 excel 字母

$i = 0;
foreach ($cellName as $k => $v) {

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$i] . $topNumber, $v[0]);//设置表头数据
    $objPHPExcel->getActiveSheet()->getStyle($cellKey[$i] . $topNumber)->getFont()->setBold(true);//设置是否加粗
    $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$i])->setWidth($v[2]);//设置列宽度
    //添加边框
    $objPHPExcel->getActiveSheet()->getStyle($cellKey[$i] . $topNumber)->applyFromArray($styleThinBlackBorderOutline);
    //设置字体 为10号
    $objPHPExcel->getActiveSheet()->getStyle($cellKey[$i] . $topNumber)->getFont()->setSize(10);
    // 设置所有垂直居中
    $objPHPExcel->getActiveSheet()->getStyle($cellKey[$i] . $topNumber)->getAlignment()
        ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

    // 居中
    $objPHPExcel->getActiveSheet()->getStyle($cellKey[$i] . $topNumber)->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getRowDimension($topNumber)->setRowHeight(24);
    $i++;
}
//处理数据
foreach ($new_item as $k => $v) {
    // 当前行数值
    $now_line = $k + 1 + $topNumber;
    $ii = 0;
    foreach ($cellName as $k1 => $v1) {
        $objPHPExcel->getActiveSheet(0)->setCellValue($cellKey[$ii] . $now_line, $v[$v1[1]]);
        $objPHPExcel->getActiveSheet(0)->getStyle($cellKey[8] . $now_line)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);// 设置文字颜色
        $ii++;
    }

    // 设置所有垂直居中

    $objPHPExcel->getActiveSheet()->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getAlignment()
        ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    // 居中
    $objPHPExcel->getActiveSheet()->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    //添加边框
    $objPHPExcel->getActiveSheet()->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->applyFromArray($styleThinBlackBorderOutline);
    $objPHPExcel->getActiveSheet()->getStyle('A' . $now_line . ':' . $lastCell . $now_line)->getFont()->setSize(12);

    $objPHPExcel->getActiveSheet()->getRowDimension($now_line)->setRowHeight(27);


//    $objPHPExcel->getActiveSheet()->getStyle('H' . $now_line)->getAlignment()->setWrapText(true);
//    $objPHPExcel->getActiveSheet()->getStyle('M' . $now_line)->getAlignment()->setWrapText(true);


}

$xlsTitle = $dtime . '拜访列表';
ob_end_clean();
//清除缓冲区,避免乱码
header('Content-type: application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition: attachment; filename=$xlsTitle.xls");
header('Cache-Control: max-age=0');
//header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//header ('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');


exit
?>
